package org.flexspeedup.blog.admin.views
{
import flash.events.MouseEvent;

import org.flexspeedup.blog.admin.events.CreateNewPostEvent;
import org.flexspeedup.blog.admin.events.DeleteSelectedPostEvent;
import org.flexspeedup.blog.admin.events.EditSelectedPostEvent;
import org.flexspeedup.blog.events.GetPostsEvent;
import org.flexspeedup.blog.events.SelectPostEvent;
import org.flexspeedup.blog.models.MiniPost;
import org.flexspeedup.model.IListModel;

import spark.components.Button;
import spark.components.List;
import spark.events.IndexChangeEvent;

/**
 * List of posts, enables edition and creation of new posts
 */
public class PostList extends List
{
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new instance of this class.
     */
    public function PostList()
    {
        super();
    }
    
    //--------------------------------------------------------------------------
    //
    //  Skin parts
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  editButton
    //----------------------------------
    [SkinPart(required="true")]
    public var editButton:Button;

    //----------------------------------
    //  deleteButton
    //----------------------------------
    [SkinPart(required="true")]
    public var deleteButton:Button;

    //----------------------------------
    //  newButton
    //----------------------------------
    [SkinPart(required="true")]
    public var newButton:Button;

    //----------------------------------
    //  previousPageButton
    //----------------------------------
    [SkinPart(required="true")]
    public var previousPageButton:Button;

    //----------------------------------
    //  nextPageButton
    //----------------------------------
    [SkinPart(required="true")]
    public var nextPageButton:Button;
    
    //----------------------------------
    //  postList
    //----------------------------------
    [SkinPart(required="true")]
    public var postList:List;
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  model
    //----------------------------------
    /**
     *  @private
     */    
    private var _model:IListModel;
    
    //----------------------------------
    //  modelChanged
    //----------------------------------
    /**
     *  @private
     */
    private var modelChanged:Boolean;
    
    [Bindable]
    /**
     * The model for this component
     */
    public function get model():IListModel
    {
        return _model;
    }
    
    public function set model(value:IListModel):void
    {
        if (_model == value)
            return;

        _model = value;
        modelChanged = true;
        invalidateProperties();
    }

    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - SkinnableComponent
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function partAdded(partName:String, instance:Object):void
    {
        switch (instance)
        {
            case (editButton):
            {
                editButton.addEventListener(
                    MouseEvent.CLICK, editButton_clickHandler);
                break;
            }
            case (deleteButton):
            {
                deleteButton.addEventListener(
                    MouseEvent.CLICK, deleteButton_clickHandler);
                break;
            }
            case (newButton):
            {
                newButton.addEventListener(
                    MouseEvent.CLICK, newButton_clickHandler);
                break;
            }
            case (previousPageButton):
            {
                previousPageButton.addEventListener(
                    MouseEvent.CLICK, previousPageButton_clickHandler);
                break;
            }
            case (nextPageButton):
            {
                nextPageButton.addEventListener(
                    MouseEvent.CLICK, nextPageButton_clickHandler);
                break;
            }
            case (postList):
            {
                postList.addEventListener(
                    IndexChangeEvent.CHANGE, postList_changeHandler);
                break;
            }
        }
    }

    /**
     *  @private
     */
    override protected function partRemoved(partName:String, instance:Object):void
    {
        switch (instance)
        {
            case (editButton):
            {
                editButton.removeEventListener(
                    MouseEvent.CLICK, editButton_clickHandler);
                break;
            }
            case (deleteButton):
            {
                deleteButton.removeEventListener(
                    MouseEvent.CLICK, deleteButton_clickHandler);
                break;
            }
            case (newButton):
            {
                newButton.removeEventListener(
                    MouseEvent.CLICK, newButton_clickHandler);
                break;
            }
            case (previousPageButton):
            {
                previousPageButton.removeEventListener(
                    MouseEvent.CLICK, previousPageButton_clickHandler);
                break;
            }
            case (nextPageButton):
            {
                nextPageButton.removeEventListener(
                    MouseEvent.CLICK, nextPageButton_clickHandler);
                break;
            }
            case (postList):
            {
                postList.removeEventListener(
                    IndexChangeEvent.CHANGE, postList_changeHandler);
                break;
            }
        }
    } 
    
    //--------------------------------------------------------------------------
    //
    //  Event Handlers
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  editButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function editButton_clickHandler(event:MouseEvent):void
    {
        dispatchEvent(new EditSelectedPostEvent())
    }
    
    //----------------------------------
    //  deleteButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function deleteButton_clickHandler(event:MouseEvent):void
    {
        dispatchEvent(new DeleteSelectedPostEvent());
    }

    //----------------------------------
    //  newButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function newButton_clickHandler(event:MouseEvent):void
    {
        dispatchEvent(new CreateNewPostEvent())
    }
    
    //----------------------------------
    //  previousPageButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function previousPageButton_clickHandler(event:MouseEvent):void
    {
        dispatchEvent(new GetPostsEvent(model.resultSet.previousPage));
    }
    
    //----------------------------------
    //  nextPageButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function nextPageButton_clickHandler(event:MouseEvent):void
    {
        dispatchEvent(new GetPostsEvent(model.resultSet.nextPage));
    }

    //----------------------------------
    //  postList_changeHandler
    //----------------------------------
    /**
     * @private
     */
    private function postList_changeHandler(event:IndexChangeEvent):void
    {
        dispatchEvent(new SelectPostEvent(
            postList.dataProvider.getItemAt(event.newIndex) as MiniPost));
    }
}
}